<template>
  <div class="jsqk-root">
    <base-table-detail :table-column="tableColumn" :form-filed="formFiled" :mid="mid" :query-list-action="queryListAction" :rules="rules"
                       :submit-data-action="submitDataAction" :submit-before-action="submitBeforeAction" :load-list-before="loadListBefore">

      <template v-slot:form-item>
        <el-form-item label="浇水时间" prop="date">
          <el-date-picker
            size="mini"
            v-model="formFiled.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="浇水方式" prop="wateringType">
          <el-select v-model="formFiled.wateringType" placeholder="请选择" clearable size="small">
            <el-option
              v-for="dict in wateringTypeDict"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="浇水量" prop="amount">
          <el-input type="number" v-model="formFiled.amount"></el-input>
        </el-form-item>
        <el-form-item label="浇水前" prop="jsq">
          <upload-image :limit="1" v-model="formFiled.jsq"></upload-image>
        </el-form-item>
        <el-form-item label="浇水过程" prop="jsgc">
          <upload-image :limit="1" v-model="formFiled.jsgc"></upload-image>
        </el-form-item>
        <el-form-item label="浇水后" prop="jsh">
          <upload-image :limit="1" v-model="formFiled.jsh"></upload-image>
        </el-form-item>
      </template>
    </base-table-detail>
  </div>
</template>

<script>
  import { reactive, toRefs, onMounted, ref, watch, computed } from '@vue/composition-api'
  import baseTableDetail from '../components/baseTableDetail'
  import { wateringList, wateringEdit } from '@/api/industry/cpf'
  import { getDicts } from '@/api/system/dict/data'
  import uploadImage from '@/components/UploadImage'

  export default {
    name: 'jsqk',
    props: {
      mid: {
        type: [String, Number],
        default: 1
      }
    },
    components: {
      uploadImage,
      baseTableDetail
    },
    setup(props, content) {
      let data = reactive({
        tableColumn: [{ name: '浇水时间', prop: 'date', width: 300 }, { name: '浇水方式', prop: 'wateringType' }, { name: '浇水量', prop: 'amount' },
          {
            name: '操作',
            prop: 'operation',
            type: 'slot',
            width: 100
          }],
        rules: {},
        queryListAction: wateringList,
        submitDataAction: wateringEdit,
        formFiled: {
          date: null,
          wateringType: null,
          amount: null,
          jsq: [],
          jsgc: [],
          jsh: []
        },
        wateringTypeDict: []
      })

      const loadDict = async() => {
        await getDicts('fil_watering_type').then(res => {
          data.wateringTypeDict = res.data
        })
      }

      const loadListBefore = async(response) => {
        await loadDict()
        response.rows.map(item => {
          item.wateringType = data.wateringTypeDict.find(el => item.wateringType == el.dictValue).dictLabel
          return item
        })

        return response
      }

      const submitBeforeAction = (requestData) => {
        let req = JSON.parse(JSON.stringify(requestData))
        req.photos = [{ type: 1, ...req.jsq[0] }, { type: 2, ...req.jsgc[0] }, { type: 3, ...req.jsh[0] }]
        return req
      }
      onMounted(() => {
      })
      return { ...toRefs(data), submitBeforeAction, loadListBefore }
    }
  }
</script>

<style scoped lang="scss">
  .jsqk-root {

  }
</style>
